package com.wanxi.udpsendreceive;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Send {
    public static void main(String[] args) {
        FileInputStream fileInputStream = null;
        DatagramSocket datagramSocket = null;
        try {
            File file = new File("testsend.jpg");
            fileInputStream = new FileInputStream(file);

            byte[] bytes = new byte[(int) file.length()];

            fileInputStream.read(bytes);

            System.out.println("发送端启动");

            datagramSocket = new DatagramSocket();

            InetAddress inetAddress = InetAddress.getByName("192.168.41.102");

            DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length, inetAddress, 1948);
            datagramSocket.send(datagramPacket);

            System.out.println("文件发送完毕");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (datagramSocket != null)
                    datagramSocket.close();
                if (fileInputStream != null)
                    fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

}

